//
//  CalculatorViewController.m
//  Calculator
//
//  Created by Wu Ting on 13-4-13.
//  Copyright (c) 2013年 Wu Ting. All rights reserved.
//

#import "CalculatorViewController.h"
#import "CalculatorModel.h"

@interface CalculatorViewController ()

@property (nonatomic) BOOL userIsInTheMiddleOfEnteringANumber;
@property (nonatomic, strong) CalculatorModel *calculatorModel;
@end

@implementation CalculatorViewController
@synthesize display = _display;
@synthesize subDisplay = _subDisplay;
@synthesize userIsInTheMiddleOfEnteringANumber = _userIsInTheMiddleOfEnteringANumber;
@synthesize calculatorModel = _calculatorModel;

- (CalculatorModel *)calculatorModel {
    if (!_calculatorModel) _calculatorModel = [[CalculatorModel alloc] init];
    return _calculatorModel;
}

- (void) appendSubDisplayByNSString:(NSString *)element {
    if ([self.subDisplay.text hasSuffix:@"="])
        self.subDisplay.text = [self.subDisplay.text substringToIndex:self.subDisplay.text.length - 1];
    self.subDisplay.text = [self.subDisplay.text stringByAppendingFormat:@"%@ =", element];
}

- (IBAction)digitPressed:(UIButton *)sender {
    NSString *digit = [sender currentTitle];
    if ([digit isEqualToString:@"π"]) {
        if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
        self.display.text = [NSString stringWithFormat:@"%g", 3.14];
        [self.calculatorModel pushOperand:[self.display.text doubleValue]];
        self.userIsInTheMiddleOfEnteringANumber = NO;
        [self appendSubDisplayByNSString:@"π "];
    } else if ([digit isEqualToString:@"e"]) {
        if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
        self.display.text = [NSString stringWithFormat:@"%g", 2.72];
        [self.calculatorModel pushOperand:[self.display.text doubleValue]];
        self.userIsInTheMiddleOfEnteringANumber = NO;
        [self appendSubDisplayByNSString:@"e "];
    } else if ([digit isEqualToString:@"0"]) {
        if (![self.display.text isEqualToString:@"0"]) self.display.text = [self.display.text stringByAppendingString:digit];
    } else if ([digit isEqualToString:@"."]) {
        if (![self.display.text rangeOfCharacterFromSet:[NSCharacterSet characterSetWithCharactersInString:@"."]].length) {
            self.display.text = [self.display.text stringByAppendingString:digit];
        } else {
            [self enterPressed];
            self.display.text = [NSString stringWithFormat:@"0."];
            self.userIsInTheMiddleOfEnteringANumber = YES;
        }
    }
    else {
        if (self.userIsInTheMiddleOfEnteringANumber) {
            self.display.text = [self.display.text stringByAppendingString:digit];
        } else {
            self.display.text = digit;
            self.userIsInTheMiddleOfEnteringANumber = YES;
        }
    }
}

- (IBAction)operationPressed:(UIButton *)sender {
    if (self.userIsInTheMiddleOfEnteringANumber) [self enterPressed];
    [self appendSubDisplayByNSString:sender.currentTitle];
    double result = [self.calculatorModel performOperation:sender.currentTitle];
    self.display.text = [NSString stringWithFormat:@"%g", result];
}

- (IBAction)enterPressed {
    [self.calculatorModel pushOperand:[self.display.text doubleValue]];
    self.userIsInTheMiddleOfEnteringANumber = NO;
    [self appendSubDisplayByNSString:self.display.text];
}

- (IBAction)clearPressed {
    self.display.text = [NSString stringWithFormat:@"0"];
    self.subDisplay.text = [NSString stringWithFormat:@""];
    self.userIsInTheMiddleOfEnteringANumber = NO;
}

- (IBAction)backspacePressed {
    if (self.userIsInTheMiddleOfEnteringANumber == YES) {
        if (self.display.text.length == 1) {
            self.display.text = [NSString stringWithFormat:@"0"];
        }else {
            self.display.text = [self.display.text substringToIndex:self.display.text.length - 1];
        }
    }
}

@end